package com.ycsj.goldmedalnewconcept.common.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gavin.com.library.StickyDecoration;
import com.gavin.com.library.listener.GroupListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.adapter.HelpRvAdapter;
import com.ycsj.goldmedalnewconcept.common.bean.HelpInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 通用的帮助与反馈页面
 */
public class HelpActivity extends BaseActivity implements SrfManager.OnPullRefreshListener, OnloadingMoreListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srf)
    SmartRefreshLayout srf;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoadingView;

    //空视图
    private View emptyView;

    //界面刷新的标识
    public static final int UPDATE = 0x552;

    //数据集合
    private List<HelpInfo.DataBean> oldDatas;

    //分页
    private int page = 1;

    //适配器
    private HelpRvAdapter adapter;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_help;
    }

    @Override
    protected void init() {

        //沉浸状态栏
        initStateBar();

        //初始化标题栏
        initTitle();

        //初始化下拉刷新控件
        initSrf();

        //初始化数据
        initData();

        //请求数据
        http(BqaManager.NORML);

    }

    private void initData() {
        oldDatas = new ArrayList<>();
    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("帮助与反馈");
    }


    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 设置列表
     */
    private void initRv() {
        rlLoadingView.setVisibility(View.GONE);

        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
        tvMessage.setText("暂无数据");
        adapter = new HelpRvAdapter(this, R.layout.common_rv_help, oldDatas);

        BqaManager.setRv(emptyView, this, adapter, rv);


        GroupListener groupListener = new GroupListener() {
            @Override
            public String getGroupName(int position) {
                //获取分组名
                return oldDatas.get(position).getName();
            }
        };
        StickyDecoration decoration = StickyDecoration.Builder
                .init(groupListener).setGroupBackground(000000)
                .setGroupTextColor(getResources().getColor(R.color.orange))
                //重置span（使用GridLayoutManager时必须调用）
                //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
                .build();
        //需要在setLayoutManager()之后调用addItemDecoration()
        rv.addItemDecoration(decoration);
    }


    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(final int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "2");


        HttpUtils.post(HttpAddress.HELP, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoadingView, srf, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    HelpInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, HelpInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<HelpInfo.DataBean> newDatas = info.getData();
                            if (newDatas != null) {
                                if (method == BqaManager.NORML) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);


                                    //设置数据
                                    initRv();
//                                    BqaManager.setRv(emptyView, HelpActivity.this, adapter, rv, HelpActivity.this);
                                } else {
                                    //更新数据
                                    BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                }
                            }
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }


    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }

    /**
     * 事件
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {

        //更新界面的消息
        if (null != busInfo && busInfo.type == UPDATE) {
            http(BqaManager.PULLREFRESH);
        }
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }

}
